//
//  FilterViewController.m
//  BubbleTea
//
//  Created by Mac on 15/11/19.
//  Copyright © 2015年 riverlj. All rights reserved.
//

#import "FilterViewController.h"

@interface FilterViewController ()
@property (weak, nonatomic) IBOutlet UILabel *firstPriceCategoryLabel;
@property (weak, nonatomic) IBOutlet UILabel *secondPriceCategoryLabel;
@property (weak, nonatomic) IBOutlet UILabel *thirdPriceCategoryLabel;

@property (weak, nonatomic) IBOutlet UILabel *numDealsLabel;


@property (weak, nonatomic) IBOutlet UITableViewCell *cheapVenueCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *moderateVenueCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *expensiveVenueCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *offeringDealCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *walkingDistanceCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *userTipsCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *nameAZSortCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *nameZASortCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *distanceSortCell;

@property (weak, nonatomic) IBOutlet UITableViewCell *priceSortCell;

@property (nonatomic, strong)NSSortDescriptor *selectedSortDescriptor;
@property (nonatomic, strong)NSPredicate *selectedPredicate;
@end

@implementation FilterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self populateCheapVenueCountLabel];
    [self populateModerateVenueCountLabel];
    [self populateExpensiveVenueCountLabel];
    
    [self populateDealsCountLabel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)populateDealsCountLabel{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Venue"];
    [fetchRequest setResultType:NSDictionaryResultType];
    NSExpressionDescription *sumExpressionDesc = [[NSExpressionDescription alloc]init];
    sumExpressionDesc.name = @"sumDeals";
    
    sumExpressionDesc.expression = [NSExpression expressionForFunction:@"sum:" arguments:@[[NSExpression expressionForKeyPath:@"specialCount"]]];
    [sumExpressionDesc setExpressionResultType:NSInteger32AttributeType];
    fetchRequest.propertiesToFetch = @[sumExpressionDesc];
    
    NSArray *results =  [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    NSDictionary *resultDict = results.firstObject;
    NSInteger numDeals = [resultDict[@"sumDeals"] integerValue];
    self.numDealsLabel.text = [NSString stringWithFormat:@"%ld total deals", numDeals];
}


- (IBAction)saveButtonTapped:(UIBarButtonItem *)sender {
    [self.delegate filterViewController:self didSelectPredicate:self.selectedPredicate SortDescriptor:self.selectedSortDescriptor];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSPredicate *)cheapVenuePredicate {
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"priceInfo.priceCategory == %@",@"$"];
    return predicate;
}

- (void) populateCheapVenueCountLabel {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Venue"];
    [fetchRequest setResultType:NSCountResultType];
    fetchRequest.predicate = [self cheapVenuePredicate];
    
    NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    int count = [array.firstObject intValue];
    self.firstPriceCategoryLabel.text = [NSString stringWithFormat:@"%d bubble tea places",count];
}

- (NSPredicate *)moderateVenuePredicate {
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"priceInfo.priceCategory == %@",@"$$"];
    return predicate;
}

- (void) populateModerateVenueCountLabel {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Venue"];
    [fetchRequest setResultType:NSCountResultType];
    fetchRequest.predicate = [self moderateVenuePredicate];
    
    NSArray *array = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    int count = [array.firstObject intValue];
    self.secondPriceCategoryLabel.text = [NSString stringWithFormat:@"%d bubble tea places",count];
}

- (NSPredicate *)expensiveVenuePredicate {
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"priceInfo.priceCategory == %@",@"$$$"];
    return predicate;
}

- (void) populateExpensiveVenueCountLabel {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Venue"];
    fetchRequest.predicate = [self expensiveVenuePredicate];
    
    NSInteger count = [self.managedObjectContext countForFetchRequest:fetchRequest error:nil];
    self.thirdPriceCategoryLabel.text = [NSString stringWithFormat:@"%ld bubble tea places",count];
}

-(void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == self.cheapVenueCell) {
        self.selectedPredicate = [self cheapVenuePredicate];
    }
    if (cell == self.moderateVenueCell) {
        self.selectedPredicate = [self moderateVenuePredicate];
    }
    if (cell == self.expensiveVenueCell) {
        self.selectedPredicate = [self expensiveVenuePredicate];
    }
    
    if (cell == self.offeringDealCell) {
        self.selectedPredicate = [self offeringDealPredicate];
    }
    if (cell == self.walkingDistanceCell) {
        self.selectedPredicate = [self walkingDistancePredicate];
    }
    if (cell == self.userTipsCell) {
        self.selectedPredicate = [self hasUserTipsPredicate];
    }
    
    if (cell == self.nameAZSortCell) {
        self.selectedSortDescriptor = [self nameSortDescriptor];
    }
    if (cell == self.nameZASortCell) {
        self.selectedSortDescriptor = (NSSortDescriptor *)[[self nameSortDescriptor] reversedSortDescriptor];
    }
    if (cell == self.distanceSortCell) {
        self.selectedSortDescriptor = [self distanceSortDescriptor];
    }
    if (cell == self.priceSortCell) {
        self.selectedSortDescriptor = [self priceSortDescriptor];
    }
    
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
}

- (NSPredicate *)offeringDealPredicate{
    return [NSPredicate predicateWithFormat:@"specialCount > 0"];
}

- (NSPredicate *)walkingDistancePredicate{
    return [NSPredicate predicateWithFormat:@"location.distance < 500"];
}

- (NSPredicate *)hasUserTipsPredicate{
    return [NSPredicate predicateWithFormat:@"stats.tipCount > 0"];
}

/**
 lazy var nameSortDescriptor: NSSortDescriptor = { var sd = NSSortDescriptor(key: "name",
 ascending: true,
 selector: "localizedStandardCompare:") return sd
 }()
 */
- (NSSortDescriptor *)nameSortDescriptor{
    return [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES selector:@selector(localizedStandardCompare:)];
}

- (NSSortDescriptor *)distanceSortDescriptor{
    return [[NSSortDescriptor alloc]initWithKey:@"location.distance" ascending:YES];
}

- (NSSortDescriptor *)priceSortDescriptor{
    return [[NSSortDescriptor alloc]initWithKey:@"priceInfo.priceCategory" ascending:YES ];
}




@end
